﻿using Authority.Logic;
using Authority.Model;
using Common.Library;
using Container.Library;
using Microsoft.AspNetCore.Mvc;
using Network.Library;
using System;
using Tool.Model;
namespace Web.Manage.Areas.Admin.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [Area("Admin")]
    public class LoginController : BaseController
    {

        private readonly ISysUserService userService = null; //用户逻辑
        private readonly ISysOrganizationService organizationService = null;//组织机构逻辑

        public LoginController()
        {
            //用户逻辑注入
            userService = UnityCIContainer.Instance.GetService<ISysUserService>();
            //组织机构逻辑注入
            organizationService = UnityCIContainer.Instance.GetService<ISysOrganizationService>();

        }

        #region 界面操作
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 入驻界面
        /// </summary>
        /// <returns></returns>
        public ActionResult Settled()
        {
            return View();
        }

        /// <summary>
        /// 忘记密码界面
        /// </summary>
        /// <returns></returns>
        public ActionResult ForgetPsw()
        {
            return View();
        }

        #endregion
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public FileContentResult GetValidateCode()
        {
            var resultInfo = new ResultFileContent();
            try
            {
               
                resultInfo = userService.LoadValidateCode();
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnFile(resultInfo);
        }

        ///</summary>
        ///登录
        ///</summary>
        public ContentResult LoginCheck()
        {
            var resultInfo = new ResultJsonInfo<SysUserInfo>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<LoginRequest>>();

                resultInfo = userService.UserLogin(parametersInfo.parameters);

                //验证通过
                if (resultInfo != null && resultInfo.Code == ActionCodes.Success)
                {
                    if (resultInfo.Success)
                    {
                        UserSession.AddLoginUser<SysUserInfo>(resultInfo.Data);
                    }
                }
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #region 入驻申请

        /// <summary>
        /// 发送短信息
        /// </summary>
        /// <returns></returns>
        public ContentResult SendMessage()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                resultInfo = organizationService.SendMessage(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 入驻申请
        /// </summary>
        /// <returns></returns>
        public ContentResult SettledApply()
        {

            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<OrganizationSettledRequest>>();

                resultInfo = organizationService.SettledApply(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion


        #region 密码找回

        /// <summary>
        /// 发送短信息
        /// </summary>
        /// <returns></returns>
        public ContentResult ForgetPswSendMessage()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();


                resultInfo = userService.ForgetPswSendMessage(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }


        /// <summary>
        /// 重设密码
        /// </summary>
        /// <returns></returns>
        public ContentResult ResetPassword()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserForgetPswRequest>>();

                resultInfo = userService.ResetPassword(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        
        #endregion
    }
}